package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.YxyEsbagTeachingPerformance;
import com.china08.yxyapi.entity.mg.esbag.test.YxyEsbagTeachingTestQuesStu;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingPerformanceRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestQuesStuRepository;

/**
 * 老师 ---备授课--授课报告--批阅作答详情
 * 
 */
@CrossOrigin
@RestController
public class LectureReportClassCorrectController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingTestQuesStuRepository yxyEsbagTeachingTestQuesStuRepository;

    @Autowired
    private YxyEsbagTeachingPerformanceRepository yxyEsbagTeachingPerformanceRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/testreport/class/corrent/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String id = reqModel.id;
        Assert.notNull(StringUtils.defaultIfBlank(id, null), "授课包ID为空");
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String classId = reqModel.classId;
        String quesGroupId = reqModel.quesGroupId;
        String quesId = reqModel.quesId;
        String studentId = reqModel.studentId;
        YxyEsbagTeachingTestQuesStu testQuesStus = yxyEsbagTeachingTestQuesStuRepository
                .findByPackIdAndTeacherIdAndSchoolIdAndQuesGroupIdAndQuesIdAndClassIdAndStudentId(id, userId, schoolId,
                        quesGroupId, quesId, classId, studentId);
        YxyEsbagTeachingPerformance teachingPerformance = yxyEsbagTeachingPerformanceRepository
                .findBySchoolIdAndTeacherIdAndPackIdAndStudentIdAndClassId(schoolId, userId, id, studentId, classId);
        RespModel respModel = new RespModel();
        Integer mark = testQuesStus.getMark();
        respModel.avantar = teachingPerformance.getAvantar();
        respModel.knowsNameList = testQuesStus.getKnowsName();
        respModel.knowsIdList = testQuesStus.getKnowsId();
        respModel.name = testQuesStus.getStudentNick();
        respModel.quesImage = testQuesStus.getQuesImage();
        if (testQuesStus.getIsReadOver() == 0) {
            mark = -1;
        }
        respModel.mark = mark;
        respModel.errorKnowId = testQuesStus.getErrorKnowId();
        respModel.wrongKnowName = testQuesStus.getWrongKnowName();
        respModel.answerOfMy = testQuesStus.getAnswerOfMy();
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 授课包id
        public String id;
        // 学生Id
        public String studentId;
        // 试题id
        public String quesId;
        // 题组id
        public String quesGroupId;
        // 班级id
        public String classId;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 头像
        public String avantar;
        // 用户名
        public String name;
        // 知识点
        public List<String> knowsNameList;
        // 知识点id
        public List<String> knowsIdList;
        // 图片
        public List<String> quesImage;
        // 评分
        public Integer mark;
        // 错误知识点id
        public List<String> errorKnowId;
        // 错误知识点名称
        public List<String> wrongKnowName;
        // 我的答案
        public String answerOfMy;

    }
}
